#include <cassert>
#include <astree/xabstractsyntaxtreecreator_assign.h>

bool XAbstractSyntaxTreeCreator_Assign::isMatched(const XTokenBlock& tokens, uint32_t row, uint32_t col) {
    auto list = tokens.at(row).tokens;
    for (size_t i = 0; i < list.size(); i++) {
        if (i == 0 && list.at(0)->getString() == L"let")
            return false;
        if (i > 0 && list.at(i)->getString() == L"=")
            return true;
    }
    return false;
}

std::shared_ptr<XAbstractSyntaxTree> XAbstractSyntaxTreeCreator_Assign::create(const XTokenBlock& tokens, uint32_t row, uint32_t col, std::shared_ptr<XAbstractSyntaxTreeBuilder> builder) {
    skipToCol = 0;
    skipToRow ++;

    auto list = tokens.at(row);
    bool findEqual = false;

    std::vector<XTokenPtr> variables, values;
    for (size_t i = 0; i < list.tokens.size(); i++) {
        auto token = list.tokens.at(i);
        if (findEqual == false && token->getString() == L"=")
            findEqual = true;
        else if (findEqual == false)
            variables.push_back(token);
        else if (findEqual)
            values.push_back(token);
    }

    assert(findEqual && "XAbstractSyntaxTreeCreator_Assign not find =");

    if (variables.empty() || values.empty()) {
        XScriptExecutor::getInstance()->setError(XError::XError_BadBlockError, list.tokens.at(0)->toString());
        return nullptr;
    }

    /*XAbstractSyntaxTreeParser parser;
    auto variable = parser.parse(variables);
    auto value = parser.parse(values);
    if (variable && value) {
        auto let = std::make_shared<XAbstractSyntaxTreeLet>(list.tokens.at(0));
        let->addChild(variable);
        let->addChild(value);
        return let;
    }*/
    return nullptr;
}
